/**
 * 
 */
package com.xbang.attendance.entity.stock;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.apache.poi.hssf.record.formula.functions.If;

import com.xbang.attendance.entity.order.DeliveryDetails;
import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.security.support.SecurityUserHolder;

import enumeration.InventoryOperation;

/**
 * 库存
 * 
 * @author dev.zhang
 */
@Entity
public class Inventory implements Serializable {

	private static final long serialVersionUID = -6307139894842251958L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/** 产品 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = Product.class)
	@JoinColumn(columnDefinition = "product", referencedColumnName = "id")
	private Product product;

	/** 仓库 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "warehouse", referencedColumnName = "id")
	private Warehouse warehouse;

	/** 当前库存数量 */
	private BigDecimal amount;

	/** 约定数量 */
	private BigDecimal agreementAmount;
	/**虚拟数量*/
	private BigDecimal virtualAmount;
	/**真实的库存数量*/
	private BigDecimal realAmount;
	/**
	 * 已出库数量
	 */
	private BigDecimal outAmount;
	/** 最后操作日期 */
	private Date lastInventoryOperationDate;

	/** 最后操作人 */
	private String lastInventoryOperationer;

	/** 调货信息 */
	private BigDecimal exchangeInventory;

	/** 预警信息 */
	@OneToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = InventoryWarningRecord.class)
	@JoinColumn(columnDefinition = "inventoryWarningRecord", referencedColumnName = "id")
	private InventoryWarningRecord inventoryWarningRecord;

	/**
	 * 产品出库
	 * 
	 * @param amount
	 * @return
	 */
	public Inventory changeProduct(BigDecimal amount, InventoryOperation inventoryOperation) {
		if (this.amount == null) {
			this.amount = new BigDecimal(0);
		}
		if (inventoryOperation == null || inventoryOperation.compareTo(InventoryOperation.OUT) == 0) {
			this.amount = this.amount.subtract(amount);
		} else {
			this.amount = this.amount.add(amount);
		}
		return updateOperation(SecurityUserHolder.getCurrentUser().getName());
	}

	/**
	 * 更新操作
	 * 
	 * @param operationer
	 * @return
	 */
	public Inventory updateOperation(String operationer) {
		this.lastInventoryOperationDate = new Date();
		this.lastInventoryOperationer = operationer;
		return this;
	}

	/**
	 * 初始化
	 * 
	 * @param warehouse
	 * @param product
	 * @param amount
	 * @return
	 */
	public Inventory initial(Warehouse warehouse, Product product, BigDecimal amount) {
		this.amount = amount;
		this.warehouse = warehouse;
		this.product = product;
		return updateOperation(SecurityUserHolder.getCurrentUser().getName());
	}
	
	/**
	 * 初始化
	 * 
	 * @param warehouse
	 * @param product
	 * @param amount
	 * @return
	 */
	public Inventory initial(Warehouse warehouse, Product product) {
		this.warehouse = warehouse;
		this.product = product;
		return updateOperation(SecurityUserHolder.getCurrentUser().getName());
	}

	/**
	 * The default constructor
	 */
	public Inventory() {

	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the amount
	 */
	public BigDecimal getAmount() {
		return amount;
	}

	/**
	 * @param amount
	 *            the amount to set
	 */
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	/**
	 * @return the lastInventoryOperationDate
	 */
	public Date getLastInventoryOperationDate() {
		return lastInventoryOperationDate;
	}

	/**
	 * @param lastInventoryOperationDate
	 *            the lastInventoryOperationDate to set
	 */
	public void setLastInventoryOperationDate(Date lastInventoryOperationDate) {
		this.lastInventoryOperationDate = lastInventoryOperationDate;
	}

	/**
	 * @return the lastInventoryOperationer
	 */
	public String getLastInventoryOperationer() {
		return lastInventoryOperationer;
	}

	/**
	 * @param lastInventoryOperationer
	 *            the lastInventoryOperationer to set
	 */
	public void setLastInventoryOperationer(String lastInventoryOperationer) {
		this.lastInventoryOperationer = lastInventoryOperationer;
	}

	/**
	 * @return the agreementAmount
	 */
	public BigDecimal getAgreementAmount() {
		return agreementAmount;
	}

	/**
	 * @param agreementAmount
	 *            the agreementAmount to set
	 */
	public void setAgreementAmount(BigDecimal agreementAmount) {
		this.agreementAmount = agreementAmount;
	}

	/**
	 * @return the inventoryWarningRecord
	 */
	public InventoryWarningRecord getInventoryWarningRecord() {
		return inventoryWarningRecord;
	}

	/**
	 * @param inventoryWarningRecord
	 *            the inventoryWarningRecord to set
	 */
	public void setInventoryWarningRecord(InventoryWarningRecord inventoryWarningRecord) {
		this.inventoryWarningRecord = inventoryWarningRecord;
	}

	/**
	 * @return the exchangeInventory
	 */
	public BigDecimal getExchangeInventory() {
		return exchangeInventory;
	}

	/**
	 * @param exchangeInventory
	 *            the exchangeInventory to set
	 */
	public void setExchangeInventory(BigDecimal exchangeInventory) {
		this.exchangeInventory = exchangeInventory;
	}

	public BigDecimal getVirtualAmount() {
		return virtualAmount;
	}

	public void setVirtualAmount(BigDecimal virtualAmount) {
		this.virtualAmount = virtualAmount;
	}

	public BigDecimal getOutAmount() {
		return outAmount;
	}

	public void setOutAmount(BigDecimal outAmount) {
		this.outAmount = outAmount;
	}

	public BigDecimal getRealAmount() {
		return realAmount;
	}

	public void setRealAmount(BigDecimal realAmount) {
		this.realAmount = realAmount;
	}
	public String getClassName(){
		if(this.amount.doubleValue() < 0)
			return "err";
		return "";
	}

}
